<template>
  <div ref="wrapper">
    <slot></slot>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
export default {
  data() {
    return {
    }
  },
  props: {
  probeType: {
    type: Number,
    default: 1
  },
  click: {
    type: Boolean,
    default: false
  },
  data: {
    type: Array,
    default: null
  },
  refreshDelay: {
    type: Number,
    default: 20
  },
  listenScroll: { // 滚动监听
    type: Boolean,
    default: false
  }
},
  components: {},
  created() {},
  mounted() {
    setTimeout(() => {
      this._initScroll()
    }, this.refreshDelay)
  },
  methods: {
    _initScroll() {
      if (!this.$refs.wrapper) {
        return
      }
      this.scroll = new BScroll(this.$refs.wrapper, {
        probeType: this.probeType,
        click: this.click
      })
      if (this.listenScroll) {
        let _this = this
        this.scroll.on('scroll', (pos) => {
          // console.log('pos', pos)
          _this.$emit('scroll',pos) //派发scroll事件
        })
      }
    },
    enable() {
      this.scroll && this.scroll.enable()
    },
    disable() {
      this.scroll && this.scroll.disable()
    },
    refresh() {
      this.scroll && this.scroll.refresh()
    },
    scrollTo() {
      this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
    },
    scrollToElement() {
      this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
    }
  },
  watch: {
    data() {
      setTimeout(() => {
        this.refresh()
      }, this.refreshDelay)
    }
  }
}
</script>

<style scoped lang='stylus'>
</style>